<template>
  <h3>虚拟表格</h3>
  <div class="table-container">
    <table class="table-head" border>
      <thead>
        <tr>
          <th>Date</th>
          <th>Name</th>
          <th>Address</th>
        </tr>
        shixi
      </thead>
    </table>
    <div class="table-body" @scroll="handleScroll">
      <table border class="scroll-table" height="100%">
        <tbody>
          <tr v-for="(item, index) in tableData" :key="index">
            <td>{{ item.date }}</td>
            <td>{{ item.name }}</td>
            <td>{{ item.address }}</td>
          </tr>
          <div class="bar"></div>
        </tbody>
      </table>
      <table border class="scroll-table-show" height="100%">
        <tbody>
          <tr v-for="(item, index) in virtalTable" :key="index">
            <td>{{ item.date }}</td>
            <td>{{ item.name }}</td>
            <td>{{ item.address }}</td>
          </tr>
          <div class="bar"></div>
        </tbody>
      </table>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { table } from "console"
import { tr } from "element-plus/es/locale"
import { ref, onMounted, computed } from "vue"
//容器高度
let containerHeight = ref<number>(0)
//滑动高度
let scrollHeight = ref<number>(0)
//全部数据
let tableData = ref<Person[]>([])
//展示虚拟数据
let virtalTable = ref<Person[]>([])
//tr高度
let trHeight = ref<number>(0)
//可以容纳多少条数据
let showNum = computed(() => {
  return Math.ceil(containerHeight.value / trHeight.value)
})
//展示数据的开始下标
let start = ref<Number>(0)
//每次展示10条数据，展示数据的结束下标
let end = ref<Number>(10)

let top = ref<number>(0)
onMounted(() => {
  const ele = document.querySelector(".table-body")
  containerHeight.value = ele.offsetHeight
  const tr = document.querySelector("tr")
  trHeight.value = tr.offsetHeight

  const table = document.querySelector("tbody")
  scrollHeight.value = table.offsetHeight
  console.log(
    containerHeight.value,
    trHeight.value,
    showNum.value,
    scrollHeight.value
  )

  renderData(start.value, end.value, tableData.value)
})

interface Person {
  date: string
  name: string
  address: string
}
generateTableData()
function generateTableData() {
  let data = []
  for (let index = 0; index < 10000; index++) {
    data.push({
      date: "2016-05-03",
      name: "Tom" + index,
      address: "No. 189, Grove St, Los Angeles",
    })
  }
  tableData.value = data
}

//处理滚动事件
function handleScroll(evt) {
  //获取滑动距离
  const scrollTop = document.querySelector(".table-body").scrollTop

  start.value = Math.ceil(scrollTop / trHeight.value)
  end.value = Math.ceil((scrollTop + containerHeight.value) / trHeight.value)
  // top.value = screenTop
  document.querySelector(".scroll-table-show ").style.top = scrollTop + "px"

  renderData(start.value, end.value, tableData.value)
}

//加载表格数据
function renderData(start: number, end: number, tableData: Person[]) {
  virtalTable.value = tableData.slice(start, end)
  console.log(virtalTable.value)
}
</script>

<style lang="scss">
html,
body {
  height: 500px;
}
.table-container {
  height: 500px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  .table-head {
    background-color: #e1f3d8;
  }
  .table-body {
    position: relative;
    flex: 1;
    background-color: #faecd8;
    overflow-y: auto;
  }
  .scroll-table {
    visibility: hidden;
  }
  .scroll-table-show {
    position: absolute;
    left: 0;
    right: 0;
  }
}
table {
  td,
  th {
    padding: 8px 0;
    border-color: #dcdfe6;
    border-width: 1px solid;
  }
  td:nth-child(2),
  th:nth-child(2),
  td:nth-child(1),
  th:nth-child(1) {
    width: 200px;
  }

  border-collapse: collapse;
  width: 100%;
}
</style>
